//
//  CinemaViewController.m
//  HuiWenMovie
//
//  Created by niceDay on 15/7/17.
//  Copyright (c) 2015年 computer. All rights reserved.
//

#import "CinemaViewController.h"
#import "cinemaCell.h"
#import "common.h"
#import "DataService.h"
#import "adressModal.h"
#import "UIImageView+WebCache.h"
#import "CinemaModal.h"

@interface CinemaViewController ()
{
    UITableView *_tableView;
    BOOL flag[100];
    NSMutableArray *_addressArray;
    NSMutableArray *_cinemaModalArray;
    NSMutableArray *_dataArray;
  
}
@end

@implementation CinemaViewController
#pragma mark - mainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.title = @"影院";

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createTableView];
    [self _initData];
    [self _loadAddressData];
    [self _loadListData];
    [self _createDataArray];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - _createSubviews
- (void)_createTableView{
    self.navigationController.navigationBar.translucent = YES;
    self.tabBarController.tabBar.translucent = YES;
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor blackColor];
    UINib *nib = [UINib nibWithNibName:@"cinemaCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:@"cellId"];
    [self.view addSubview:_tableView];
}
#pragma mark - init
- (void)_initData{
    _addressArray = [[NSMutableArray alloc] init];
    _cinemaModalArray = [[NSMutableArray alloc] init];
     _dataArray = [[NSMutableArray alloc] init];
}

#pragma mark - _loadData

- (void)_loadAddressData{
  
    NSDictionary *dic = [DataService initWitnJsionFileName:addressFile];
    NSArray *array = [dic objectForKey:@"districtList"];
    for (NSDictionary *contentDic in array) {
        adressModal *modal = [[adressModal alloc] init];
        [modal setValuesForKeysWithDictionary:contentDic];
        [_addressArray addObject:modal];
    }
}

- (void)_loadListData{
    NSDictionary *dic = [DataService initWitnJsionFileName:cinemaListFile];
    NSArray *cinemaList = [dic objectForKey:@"cinemaList"];
    for (NSDictionary *contentDic in cinemaList) {
        CinemaModal *modal = [[CinemaModal alloc] init];
        [modal setValuesForKeysWithDictionary:contentDic];
        [_cinemaModalArray addObject:modal];
          }
    
}

- (void)_createDataArray{
    
    for (int i = 0; i < _addressArray.count; i++) {
       
        NSMutableDictionary *dataDic = [[NSMutableDictionary alloc] init];
        NSMutableArray *array = [[NSMutableArray alloc] init];
        NSString *str = [_addressArray[i] addressId];
        for (int j = 0 ; j< _cinemaModalArray.count; j++) {
            if ([[_cinemaModalArray[j] districtId] isEqualToString:str] )
            {
                
                [array addObject:_cinemaModalArray[j]];
        
            }
        }
        [dataDic setValue:[_addressArray[i] name] forKey:@"name"];
        [dataDic setObject:array forKey:@"cinema"];
        [_dataArray addObject:dataDic];
     }
  
}
#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *modalArray = [_dataArray[section] objectForKey:@"cinema"];
    if (flag[section] == 0) {
        return 0;
    }
    else{
      return [modalArray count];
   }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
   return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    cinemaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];
     NSArray *numArray = [_dataArray[indexPath.section] objectForKey:@"cinema"];
    cell.modal = numArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 96;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, Kwidth, 40)];
    NSString *name = [_dataArray[section] objectForKey:@"name"];
    [button setTitle:name forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    NSLog(@"%@",[_dataArray[section] objectForKey:@"name"]);
     
     [button setBackgroundImage:[UIImage imageNamed:@"hotMovieBottomImage"] forState:UIControlStateNormal];
    button.titleEdgeInsets = UIEdgeInsetsMake(5, Kwidth/2-45, 5,Kwidth/2-45);
    button.backgroundColor = [UIColor blueColor];
    button.tag = section;
    
    [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

//#pragma mark - action
- (void)btnAction:(UIButton *)btn{
    NSInteger index = btn.tag;
    flag[index] = !flag[index];
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
    
    [_tableView reloadSections:indexSet withRowAnimation: UITableViewRowAnimationFade];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
